<?php

namespace app\modules\Income\controllers;

use Yii;
use app\modules\Income\models\IncomeClaim;
use app\modules\Income\models\IncomeClaimSearch;
use app\modules\Income\models\Income;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * 收入认领管理
 */
class ClaimController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    //认领列表
    public function actionIndex($income_id)
    {
        $searchModel = new IncomeClaimSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$income_id);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'income_id' => $income_id,
        ]);
    }

    //查看认领
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    //创建认领
    public function actionCreate($income_id)
    {
        $model = new IncomeClaim();

        if ($model->load(Yii::$app->request->post())) {
            $income = Income::findOne($income_id);

            $model->rate = $income->rate;
            $model->pro_price = round($model->price*$model->rate,2);

            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }

        }else{
            
            $model->income_id = $income_id;
            $model->currency = 1;
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    //确认 收入认领
    public function actionConfirm($id){
        $model = $this->findModel($id);
        $model->status = 2;
        $model->confirm_at = date("Y-m-d H:i:s");
        $model->confirm_id    = Yii::$app->user->id;
        if($model->save()){
            return $this->redirect(Yii::$app->request->getReferrer());
        }
        throw new NotFoundHttpException(Yii::t('app', '确认错误'));
    }

    //更新认领
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        $id =\yii::$app->request->get('id');

        if( Yii::$app->user->id != $model->user_id){
            throw new NotFoundHttpException(Yii::t('app', '没有权限修改！'));
        }

        if ($model->load(Yii::$app->request->post())) {
            
            $income = Income::findOne($model->income_id);

            $model->rate = $income->rate;
            $model->pro_price = round($model->price*$model->rate,2);

            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
           
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

   
    protected function findModel($id)
    {
        if (($model = IncomeClaim::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }


}
